package vfs.common.command.remote;

import vfs.common.Locale;
import vfs.common.command.AbstractCommand;
import vfs.common.file.VirtualFile;
import vfs.common.file.VirtualFileNotFoundException;

/**
 * User: serge
 * Date: 22.11.13
 * Time: 5:43
 */
public class CopyCommand extends AbstractCommand implements RemoteCommand
{

    @Override
    public void execute()
    {
        checkTty();
        checkUser();
        checkFilesystem();

        if (params.length != 3)
        {
            tty.broadcast(Locale.CMD_EXC_WRONG_PARAMS, user);
        }

        String pathSource = params[1];
        String pathDest = params[2];

        try
        {
            VirtualFile source = filesystem.locateFile(pathSource, user);
            VirtualFile dest = filesystem.locateFile(pathDest, true, user);

            if (dest.getData().containsKey(source.getName()))
            {
                tty.send("Such dir/file already exists in destination", user);
            }
            else
            {
                dest.getData().put(source.getName(), source);
                tty.broadcast(String.format("%s performs command: CP %s %s", user.getName(), pathSource, pathDest),
                        user);
                tty.send("Copied ok", user);
            }
        }
        catch (VirtualFileNotFoundException e)
        {
            e.printStackTrace();
        }

        //Todo generated method
    }
}
